package com.ly.loginstarter.dao;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Repository
public class LoginDao {

    @Value("${ly.login.table-user}")
    private String tableUser;
    @Value("${ly.login.column-name}")
    private String columnName;
    @Value("${ly.login.column-pwd}")
    private String columnPwd;
    @Resource
    private JdbcTemplate jdbcTemplate;

    public List<Map<String, Object>> selectForLogin(
            String username, String password) {
        String sql = "select * from %s where %s = ? and %s = ?";
        sql = String.format(sql, tableUser, columnName, columnPwd);
        return jdbcTemplate.queryForList(sql, username, password);
    }

    // 根据用户名查询用户信息 ( 用户名必须是唯一索引字段 )
    public Map<String,Object> selectByName(String username){
        String sql = "select * from %s where %s = ?";
        sql = String.format(sql, tableUser, columnName);
        return jdbcTemplate.queryForMap(sql, username);
    }

    // 根据用户名查询用户信息 ( 用户名必须是唯一索引字段 )
    public void updatePwd(String username, String pwd){
        String sql = "update %s set %s = ? where %s = ?";
        sql = String.format(sql, tableUser, columnPwd, columnName);
        jdbcTemplate.update(sql, pwd,  username);
    }
}
